/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.services.servlets;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.phonebooksharing.dto.GenericTransferDTO;
import com.phonebooksharing.dto.gallery.WishlistItem;
import com.phonebooksharing.services.ejb3.facade.ProfileFacade;

/**
 * Servlet implementation class DashboardServlet
 */
public class WishlistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public WishlistServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//	    PrintWriter pw = response.getWriter();
//        response.setContentType("text/html");
//        pw.print("<html><head/><body><h1>Welcome to Phonebook Sharing Services! </h1></body></html>");
	    doPost(request, response);
	}
	
	@EJB
    private ProfileFacade profileFacade;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    
	    try {
	        ObjectInputStream ois = new ObjectInputStream(request.getInputStream());
            final String ac = request.getParameter("ac");
            if("ms".equalsIgnoreCase(ac)) {//manage sharing
                final String what = request.getParameter("q");
                if("awl".equalsIgnoreCase(what)) {//add to wishlist
                    ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
                    GenericTransferDTO gt = (GenericTransferDTO)ois.readObject();
                    ArrayList<WishlistItem> list = (ArrayList<WishlistItem>)gt.items2;
                    
                    oos.writeObject(profileFacade.addProductToWishlist(gt.authItem, list));
                } else if("s".equalsIgnoreCase(what)) {
                    final String authItem = (String) ois.readObject();
                    
                    ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
                    oos.writeObject(profileFacade.getSharingWishlist(authItem));
                } else if("ns".equalsIgnoreCase(what)) {
                    final String authItem = (String) ois.readObject();
                    
                    ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
                    oos.writeObject(profileFacade.getNonSharingWishlist(authItem));
                }
            } else if("gwl".equalsIgnoreCase(ac)) {
                final String authItem = (String) ois.readObject();
                final int page     = new Integer(request.getParameter("p"));
                final int pageSize = new Integer(request.getParameter("ps"));
                final int contactID = new Integer(request.getParameter("c"));
                
                ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
                oos.writeObject(profileFacade.browseWishlistGallery(authItem, contactID, page, pageSize));
            }
	    } catch (Exception e) {
	        e.printStackTrace();
        }
	}
}
